package mk.com.ldm.corepoint.implementation.client.lib.wrappers.filters;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import mk.com.ldm.corepoint.implementation.client.CoreServiceAsync;
import mk.com.ldm.corepoint.implementation.client.lib.generators.BaseFilter;
import mk.com.ldm.corepoint.implementation.shared.DataBaseObject;
import mk.com.ldm.corepoint.implementation.shared.Filter;
import mk.com.ldm.corepoint.implementation.shared.TableColumn;

import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ListBox;

public class ListBoxFilter extends BaseFilter{
	private boolean init = true;	
	
	public ListBoxFilter(final HashMap<String, Filter> filters, final TableColumn tc, final Button filterBtn, final CoreServiceAsync rpcService) {
		super(filters, tc, filterBtn);
		final ListBox lb = new ListBox();
		lb.setWidth((tc.getWidth()+6)+"px");
		lb.addStyleName("list-box-filter");
		lb.addItem("Filter "+tc.getHeader(), "none");
		
		lb.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if(!tc.getForeign().isEmpty()){
					if(init){
						lb.addItem("Loading...");
						String listInfo = tc.getForeign().contains("FIL:")?tc.getForeign().replace("FIL:", ""):tc.getForeign();
						String[] foreign = listInfo.split("/");
						DataBaseObject id = new DataBaseObject(foreign[2], true);
						DataBaseObject name = new DataBaseObject(foreign[3], true);
						ArrayList<DataBaseObject> columns = new ArrayList<DataBaseObject>();
						columns.add(id);
						columns.add(name);
						rpcService.getKeyValuePairFromTable(foreign[0]+":"+foreign[1], columns, new AsyncCallback<HashMap<String,String>>() {
							
							@Override
							public void onSuccess(HashMap<String, String> result) {
								lb.removeItem(1);
								Set<String> keys = result.keySet();
								Iterator<String> i = keys.iterator();
								while(i.hasNext()){
									String id = i.next();
									String name = result.get(id);
									lb.addItem(name, id);
								}
							}
							
							@Override
							public void onFailure(Throwable caught) {
								// TODO Auto-generated method stub
								
							}
						});
						init = false;
					}
				}
			}
		});
		
		lb.addChangeHandler(new ChangeHandler() {
			@Override
			public void onChange(ChangeEvent event) {
				int index = lb.getSelectedIndex();
				if(!lb.getValue(index).equals("none")){
					HashMap<String, String> hm = new HashMap<String, String>();
					hm.put(lb.getItemText(index), lb.getItemText(index));
					Filter f = new Filter(tc.getHeader(), hm, tc.getFilterOperator());
					filters.put(tc.getHeader(), f);
				}
				else{
					filters.remove(tc.getHeader());
				}
				filterBtn.click();
			}
		});
		initWidget(lb);
	}

}
